package com.bp.hearthealth.pulse.lnapps.bp_network

import com.bp.hearthealth.pulse.lnapps.R
import com.bp.hearthealth.pulse.lnapps.bp_extension.UIHelperBp
import com.bp.hearthealth.pulse.lnapps.bp_extension.showToast
import com.bp.hearthealth.pulse.lnapps.bp_ui.bp_activity.bp_language_gender.bp_language.LanguageAndCity
import com.bp.hearthealth.pulse.lnapps.bp_utils.Constants

open class BaseRepository {
    suspend fun <T : Any> executeResp(
        block: suspend () -> BaseResp<T>, stateLiveData: BpStateLiveData<T>
    ) {
        var baseResp = BaseResp<T>()
        try {
            baseResp.dataState = DataState.STATE_LOADING
            val invoke = block.invoke()
            baseResp = invoke
            if (baseResp.isSuccess) {
                if (baseResp.data == null || baseResp.data is List<*> && (baseResp.data as List<*>).isEmpty()) {
                    baseResp.dataState = DataState.STATE_EMPTY
                } else {
                    baseResp.dataState = DataState.STATE_SUCCESS
                }
            } else {
                baseResp.msg?.showToast()
                baseResp.dataState = DataState.STATE_FAILED
            }
        } catch (e: Exception) {
            baseResp.dataState = DataState.STATE_ERROR
            baseResp.error = e
        } finally {
            stateLiveData.postValue(baseResp)
        }
    }

    /**
     * 请勿修改languageAndCityCodes集合顺序
     * en_US,pt_PT,ms_MY,es_ES,de_DE,it_IT,fr_FR,in_ID,ja_JP,ko_KR,tr_TR,tl_PH,hu_HU,th_TH,zh_CN,zh_TW
     */
    protected val languageAndCities = mutableListOf(
        LanguageAndCity(Constants.dfT, Constants.dfC),//系统语言
        LanguageAndCity("en", "US"),
        LanguageAndCity("pt", "PT"),
        LanguageAndCity("ms", "MY"),
        LanguageAndCity("es", "ES"),
        LanguageAndCity("de", "DE"),
        LanguageAndCity("it", "IT"),
        LanguageAndCity("fr", "FR"),
        LanguageAndCity("in", "ID"),
        LanguageAndCity("ja", "JP"),
        LanguageAndCity("ko", "KR"),
        LanguageAndCity("tr", "TR"),
        LanguageAndCity("tl", "PH"),
        LanguageAndCity("hu", "HU"),
        LanguageAndCity("th", "TH"),
        LanguageAndCity("zh", "CN"),
        LanguageAndCity("zh", "TW")
    )

    protected fun getLanguageList(): MutableList<String> {
        val languageList: MutableList<String> = ArrayList()
        languageList.add(UIHelperBp.getString(R.string._default_str))//系统语言
        languageList.add("English")//英语：English                en_US
        languageList.add("Portugués")//葡萄牙语：Portugués         pt_PT
        languageList.add("Melayu")//马来语：Melayu                 ms_MY
        languageList.add("espanol")//西班牙语：espanol             es_ES
        languageList.add("Deutsch")//德语：Deutsch                de_DE
        languageList.add("ltaliano")//意大利语：ltaliano           it_IT
        languageList.add("Français")//法语：Français              fr_FR
        languageList.add("Indonesia")//印度尼西亚：Indonesia       in_ID
        languageList.add("日本語")//日语：日本語                    ja_JP
        languageList.add("한국인")//韩语：한국인                    ko_KR
        languageList.add("Türkçe")//土耳其：Türkçe                tr_TR
        languageList.add("Filipino")//菲律宾语：Filipino           tl_PH
        languageList.add("Magyar")//匈牙利语：Magyar               hu_HU
        languageList.add("แบบไทย")//泰语：แบบไทย                  th_TH
        languageList.add("简体中文")//简体中文                      zh_CN
        languageList.add("繁體中文")//繁體中文                      zh_TW
        return languageList
    }
}